package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.MyCustomListView;
import com.xgkj.diyiketang.weidget.MyScrollView;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.SellerAdapter;
import com.xgkj.diyiketang.data.FoodInfo;
import com.xgkj.diyiketang.data.SellerInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.SellerListTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/17.
 */
public class SellerListActivity extends BaseActivity implements MyScrollView.OnScrollListener{

    private Context mContext;
    private MyCustomListView mListView;
    private SellerAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private String areaName;
    private int mSellerTask = -1;
    private TextView back ;
    private String areaId ;
    private String foodId;
    private ImageView foodImg;
    private TextView foodName;
    private TextView foodIntro;
    private MyScrollView mScrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sellerlist);
        mContext = this;
        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        foodId = intent.getStringExtra("FOODID");
        areaId = intent.getStringExtra("AREAID");
        mScrollView = (MyScrollView) findViewById(R.id.seller_scrollView);
        mScrollView.setOnScrollListener(this);
        foodImg = (ImageView) findViewById(R.id.fooddetail_logo);
        foodName = (TextView) findViewById(R.id.fooddetail_name);
        foodIntro = (TextView) findViewById(R.id.fooddetail_note);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (MyCustomListView)findViewById(R.id.sellerpulllist);
        //mListView.init(mContext,true);
        mAdapter = new SellerAdapter(mContext);

        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mListView.getHeaderViewsCount()>0){
                    position--;
                }
                if (position<0 || position>=mAdapter.getCount())return;
                SellerInfo fdinfo = (SellerInfo) mAdapter.getItem(position);
                String fdid = fdinfo.getBusinessId();
                Intent intent = new Intent(mContext, SellerDetailActivity.class);
                intent.putExtra("BSUINESSID", fdid);
                startActivity(intent);
            }
        });
//        mListView.setOnRefreshListener(this);
//        mListView.setOnLoadDataListener(this);
//        mListView.doRefresh();
        getSellerList();

    }

    private void getSellerList(){
        if(mSellerTask != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        SellerListTask stask = new SellerListTask(size,currentPage,foodId,areaId);
        stask.addListener(getSellerListListener);
        mSellerTask = getEatShowApp().getTaskManager().addTask(stask);

    }

    private TaskEventListener getSellerListListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mSellerTask){
                mSellerTask = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
//                    mListView.onRefreshComplete();
//                    mListView.enableLoadMore();
                }else{
                    // mListView.onLoadDataComplete(true);
                }
                List<SellerInfo> mfoodlist = new ArrayList<SellerInfo>();
                if(data != null){
                    // mfoodlist = (List<SellerInfo>) data;
                    int pageCount = 0;
                    int currentPage = 0;
                    int nextPage = 0;

                    JSONObject jsonString = (JSONObject) data;
                    currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
                    pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
                    nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                    String fname = jsonString.getString("foodName");
                    String flogo = jsonString.getString("foodBigLogo");
                    String fnote = jsonString.getString("foodIntroduction");
                    if(flogo != null && flogo.length()>0){
                        ImageCache.getInstance(mContext).loadBitmapToView(foodImg, flogo, ViewUtil.getImageSize(mContext,R.drawable.default_big_image));
                    }
                    ViewUtil.Size size = ViewUtil.getImageSize(mContext,R.drawable.default_big_image);
                    PalLog.e("图片大小",size.width+"******"+size.height);
                    foodName.setText(fname);
                    foodIntro.setText(fnote);
                    mfoodlist = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), SellerInfo.class);
                }

                if(mfoodlist != null && mfoodlist.size()>0){
                    mAdapter.updateData(mfoodlist);
                }

                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == mSellerTask){
                mSellerTask = -1;
//                if(currentPage == 1){
//                    mListView.onRefreshComplete();
//                }else{
//                    mListView.onLoadDataComplete(false);
//                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

//    @Override
//    public void onPullRefresh() {
//
//    }
//
//    @Override
//    public void onLoadMore() {
//        getSellerList();
//    }
//
//    @Override
//    public void onRefresh() {
//        currentPage=1;
//        getSellerList();
//    }

    public void onHiddenChanged(boolean hidden) {
        onHiddenChanged(hidden);
        if(!hidden){
            //mListView.invalidate();
            if(mAdapter != null && mAdapter.getCount() == 0){
//                mListView.doRefresh();
                currentPage =1;
                getSellerList();
            }

        }
    }

    @Override
    public void onScroll() {
        getSellerList();
    }
}
